package com.mer.handler;

import com.alibaba.fastjson.JSON;
import com.mer.utils.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class MyFailureHandler implements  AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        Result result = new Result(500, "账号或密码错误", null);
        String s = JSON.toJSONString(result);
        PrintWriter out = response.getWriter();
        response.setContentType("application/json;charset=utf-8");
        out.print(s);
        out.flush();
        out.close();
    }
}
